package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.atguigu.tingshu.album.config.MinioConstantProperties;
import com.atguigu.tingshu.album.service.FileUploadService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.execption.GuiguException;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * ClassName: FileUploadServiceImpl
 * Package: com.atguigu.tingshu.album.service.impl
 * Description:
 *
 * @Author 咚咚小圆帽
 * @Create 2025/6/24 22:49
 * @Version 1.0
 */
@Service
public class FileUploadServiceImpl implements FileUploadService {

    @Autowired
    private MinioConstantProperties properties;

    @Autowired
    private MinioClient minioClient;

    @Autowired
    private VodService vodService;

    /**
     * 文件上传
     *
     * @param file 用户上传的文件对象
     * @return
     */
    @Override
    public String fileUpload(MultipartFile file) {
        try {
            //1.验证图片文件是否合法、超过指定大小
            BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
            if (bufferedImage == null) {
                throw new GuiguException(500, "文件格式非法");
            }
            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();

            if (width > 900 || height > 900) {
                throw new GuiguException(500, "文件大小超过900*900");
            }

            //判断图片内容是否合法
            String suggest = vodService.scanImage(file);
            if("block".equals(suggest)||"review".equals(suggest)){
                throw new RuntimeException("图片内容违规,请修改！");
            }

            //2.将文件上传到minio分布式存储服务器
            //2.1 生成全局唯一名称，形式：/路径(年月日)/文件唯一名称：后缀。
            //首先是文件夹名称(使用年月日)
            String folderName = DateUtil.today().replace("-", "");
            //然后是文件名称(使用唯一id)
            String fileName = IdUtil.randomUUID();
            //接着是文件后缀(使用原始文件的后缀)
            String extName = FileUtil.extName(file.getOriginalFilename());
            //最终文件名称
            String objectName = "/" + folderName + "/" + fileName + "." + extName;


            //2.2 调用Minio客户端上传文件方法
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(properties.getBucketName())
                            .object(objectName)
                            .stream(file.getInputStream(), file.getSize(), -1)
                            .contentType(file.getContentType())
                            .build());


            //3.返回文件指定路径
            return properties.getEndpointUrl() + "/" + properties.getBucketName() + objectName;

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
